$(document).ready(function (){

	list_initEvent();
	
	list_list(true);

});

function list_initEvent()
{
	// 翻页事件
	index_initPageEvent("list_list()");
	
	// 添加按钮
	$("#add").click(function (){
		$('body').umDialog("dialog",{
			content:$("#alert_div").html(),
			enter_method:sysuser_edit,
			width:450,
			left:"40%",
			top:"15%"
		});
	});
	
	// 查询按钮
	$("#query").click(function (){
		$("#list_tbody").html("");
		list_list(true);
	});
	
	
	// 状态按钮
	$("#list_tbody").delegate("[data-id=status]" ,"click" ,function(){
		$("body").umDialog("confirm",{
			content:"确认更改么？",
			enter_method:function (self){
				var obj = new Object();
				obj.userId = $(self).parent().attr("data-id");
				obj.user_status = $(self).attr("data-value");
				um_ajax_post(
						INDEX_WEBAPP + "/sysuser/op.do",
						obj,
						function (data){
							$("body").umDialog("close");
							index_show("操作成功!");
							list_list(false);
						},
						null);
			},
			enter_method_param:this
		});
	});
	
	// 修改按钮
	$("#list_tbody").delegate("[data-id=update]" ,"click" ,function(){
		var obj = new Object();
		obj.userId = $(this).parent().attr("data-id");
		um_ajax_get(
				INDEX_WEBAPP + "/sysuser/detail.do",
				obj,
				function (data){
					$('body').umDialog("dialog",{
						content:$("#alert_edit_div").html(),
						width:450,
						left:"40%",
						top:"15%",
						enter_method:sysuser_edit
					});
					$("#dialog").umDataBind("render" , data.sysUser ,"");
				},
				null);
	});
	
	// 详情按钮
	$("#list_tbody").delegate("[data-id=detail]" ,"click" ,function(){
		var obj = new Object();
		obj.userId = $(this).parent().attr("data-id");
		um_ajax_get(
				INDEX_WEBAPP + "/sysuser/detail.do",
				obj,
				function (data){
					$('body').umDialog("alert",{
						content:$("#alert_detail_div").html(),
						width:450,
						left:"40%",
						top:"23%"
					});
					$("#dialog").umDataBind("render" , data.sysUser ,"");
				},
				null);
	});
	
	// 是否修改密码
	$("#dialog").delegate("input[type=checkbox]" ,"click" ,function (){
		if ($(this).is(':checked'))
		{
			$("#dialog").find("[id=update_pwd_div]").show();
			$("#dialog").find("[id=update_pwd_div]").find("input").attr("validate" ,"required");
		}
		else
		{
			$("#dialog").find("[id=update_pwd_div]").hide();
			$("#dialog").find("[id=update_pwd_div]").find("input").removeAttr("validate");
		}
	});
}

function list_list(isFirst)
{
	if (isFirst)
	{
		index_pageObj.init();
	}
	
	loadingStart();
	
	var obj = new Object();
	obj.queryKey = $("#queryKey").val();
	obj.currentPage = index_pageObj.currentPage;
	obj.pageSize = index_pageObj.pageSize;
	
	um_ajax_get(
			INDEX_WEBAPP + "/sysuser/select.do",
			obj,
			function (data){
				$("#page_record_count").text(data.size);
				_render(data.sysUsers);
			},
			null);
	
	function _render(data)
	{
		if (data && data.length >0)
		{
			var buffer = [];
			for (var i=0;i<data.length;i++)
			{
				buffer.push('<tr>');
				buffer.push('<td>'+(((obj.currentPage-1)*obj.pageSize)+i+1)+'</td>');
				buffer.push('<td>'+data[i].user_name+'</td>');
				buffer.push('<td>'+data[i].user_account+'</td>');
				buffer.push('<td>'+data[i].user_work_num+'</td>');
				buffer.push('<td>'+(data[i].user_status=="0"?"已启用":"已注销")+'</td>');
				buffer.push('<td style="text-align: center" data-id="'+data[i].userId+'">'+getOpButton(data[i].user_status)
						+'|'+'<a href="#" data-id="update"> 修改 </a>'+'|'+'<a href="#" data-id="detail"> 详情</a>'+'</td>');
				buffer.push("</tr>");
			}
			$("#list_tbody").html(buffer.join(''));
		}
		else
		{
			index_pageObj.toEndPage();
		}
		
		index_pageObj.isEndMethod(data);
	}
}
//获取商品操作按钮
function getOpButton(status)
{
	var buffer = [];
	
	if (0 == status)
	{
		buffer.push(' <a href="#" data-id="status" data-value="1"> 注销 </a>');
	}
	else
	{
		buffer.push(' <a href="#" data-id="status" data-value="0"> 启用 </a>');
	}
	return buffer.join("");
}

function sysuser_edit()
{
	if (!$("#dialog").validate())
	{
		return false;
	}
	var obj = $("#dialog").umDataBind("serialize" ,"");
	var url = "/sysuser/add.do";
	if (obj.userId != 0)
	{
		url = "/sysuser/update.do";
		obj.isPasswordModify = $("#isPasswordModify").is(":checked");
	}
	
	um_ajax_post(
			url,
			obj,
			function (data){
				index_show("操作成功!");
				list_list(false);
				$('body').umDialog("close");
			},
			null);
}